#include "charsmodel.h"

CharsModel::CharsModel( QObject * parent )
    : QAbstractTableModel( parent )
{
rows_count = 0;
}

CharsModel::~CharsModel()
{
}

void CharsModel::set_data( QList<QVector<int> > ranges )
{
model_data.clear();

rows_count = 0;

    if ( ranges.isEmpty() )
    {
    rows_count = 4096;
        for ( int x = 0; x < rows_count; ++x )
        {
        QMap<int, QString> m;    
            for ( int z = 0; z < 16; ++z )
            {
            m[z] = QString( QChar( ( ( x * 16 ) + z) ) );
            }
        model_data << m;
        }
    }
    else
    {

        for ( int i = 0; i < ranges.size(); ++i )
        {
        QVector<int> v = ranges.at(i);
        int start = v.at(0);
        int end = v.at(1) + 1;
        rows_count = rows_count + end - start;
            for ( int x = start; x < end ; ++x )
            {
            QMap<int, QString> m;    
                for ( int z = 0; z < 16; ++z )
                {
                m[z] = QString( QChar( ( ( x * 16 ) + z) ) );
                }
            model_data << m;
            }
        }
    }
}

int CharsModel::rowCount( const QModelIndex & ) const
{
return rows_count;
}

int CharsModel::columnCount(const QModelIndex & ) const
{
return 16;
}

QVariant CharsModel::data( const QModelIndex & index,
                           int role ) const
{
    if ( role == Qt::DisplayRole )
    {
    QString s = model_data.at( index.row() ).value( index.column()  );
    QVariant qv = QVariant(s);
    return qv;
    }
return QVariant();
}

